﻿using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using PrismWpf.Views;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace PrismWpf.ViewModels
{
/*
 要求是 public 否则无法注入 View
 */
    public class MainViewModel:BindableBase
    {
        /*private object _Body;
        public object Body { get => _Body; private set {

                _Body = value;
                RaisePropertyChanged();
            } }*/

        public ICommand ChangeRoute { get; private set; }

        public ICommand GoBack { get; }

        private IRegionManager RegionManager;

        private IRegionNavigationJournal NavigationJournal;

        public MainViewModel(IRegionManager regionManager)
        {
            ChangeRoute =  new DelegateCommand<string>(ParseRoute);
            //ParseRoute("");
            RegionManager = regionManager;

            GoBack = new DelegateCommand<Object>(Back);
        }

        private void ParseRoute(string newRoute) {

            NavigationParameters navigationParameters = new NavigationParameters();

            navigationParameters.Add("deviceName", "交换机");

            // 从列表中拿到 MainView 中对应的 Region
            RegionManager.Regions["mainViewBodyRegion"]
                // 设置此 Region 渲染其他视图
                // 使用导航就 很像  前端路由
                .RequestNavigate(newRoute, callBack => {

                    if (callBack.Result is not null && callBack.Result==true) {

                        // 更新导航信息
                        NavigationJournal = callBack.Context.NavigationService.Journal;

                    }


                },navigationParameters);
            /*switch (newRoute)
            {

                case "order": {
                        Body = new OrderView();
                        break;
                    }
                case "device": {
                        Body = new DeviceView();
                        break;
                    }

                default : { 
                        Body = new IndexView();
                        break;
                }

            }*/
        }

        private void Back(object obj) {

            if (NavigationJournal.CanGoBack)
            {
                NavigationJournal.GoBack();
            }
        }
    }
}
